package com.example.paimengshop;

import com.example.paimengshop.client_socket.ActionControl_Send;
import com.example.paimengshop.client_socket.ReceiveFromServer;
import com.example.paimengshop.client_socket.SendToServer;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.Date;
import java.util.Objects;
import java.util.ResourceBundle;

public class ResetController implements Initializable
{
    @FXML
    PasswordField passwordInput;

    @FXML
    PasswordField passwordConfirm;

    @FXML
    AnchorPane resetPane;

    @FXML
    Button submitBtn;

    @FXML
    private Button verificationBtn;

    @FXML
    private TextField verificationInput;

    @FXML
    private Button seeBtn;

    @FXML
    private Button unSeeBtn;

    @FXML
    private TextField passwordText;

    boolean isVisible = false;

    @FXML
    void look(ActionEvent event) {
        isVisible = true;
        seeBtn.setVisible(false);
        seeBtn.setManaged(false);
        passwordInput.setVisible(false);
        passwordInput.setManaged(false);
        unSeeBtn.setVisible(true);
        unSeeBtn.setManaged(true);
        passwordText.setVisible(true);
        passwordText.setManaged(true);

        passwordText.setText(passwordInput.getText());
    }

    @FXML
    void unlook(ActionEvent event) {
        isVisible = false;
        unSeeBtn.setVisible(false);
        unSeeBtn.setManaged(false);
        passwordText.setVisible(false);
        passwordText.setManaged(false);
        seeBtn.setVisible(true);
        seeBtn.setManaged(true);
        passwordInput.setVisible(true);
        passwordInput.setManaged(true);

        passwordInput.setText(passwordText.getText());
    }

    @FXML
    void getVerification(ActionEvent event)
    {
        if(isVisible){
            passwordInput.setText(passwordText.getText());
        }

        //获取验证码
        String email = Data.currentUser.getEmail();
        String password1 = passwordInput.getText();
        String password2 = passwordConfirm.getText();
        if (!password1.equals(password2))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("重置");
            alert.setContentText("两次输入的密码不一致");
            alert.setHeaderText("注意密码");
            alert.showAndWait();
        }
        else
        {
            main.getSend().Send(ActionControl_Send.ChangePassword(Data.currentUser, email, password2));
        }
    }

    @FXML
    void OnSubmitBtnClicked(ActionEvent event) {

        if(Objects.equals(verificationBtn.getText(), ""))
        {
            Alert alert =new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("重置密码");
            alert.setContentText("未输入验证码");
            alert.setHeaderText("注意");
            alert.showAndWait();
        }
        else
        {
            String email = Data.currentUser.getEmail();
            String password1 = passwordInput.getText();
            String password2 = passwordConfirm.getText();
            int verCode = Integer.parseInt(verificationInput.getText());

            main.getSend().Send(ActionControl_Send.SendVerCode_ChangePassword(email, password2, verCode));
        }

        passwordInput.clear();
        passwordConfirm.clear();
        verificationInput.clear();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {
        unSeeBtn.setVisible(false);
        unSeeBtn.setManaged(false);
        passwordText.setVisible(false);
        passwordText.setManaged(false);

        ImageView seeView=new ImageView();
        seeView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/see.png"));
        seeView.setFitWidth(15);
        seeView.setFitHeight(15);
        seeBtn.setGraphic(seeView);

        ImageView unseeView=new ImageView();
        unseeView.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/controllerImage/unsee.png"));
        unseeView.setFitWidth(15);
        unseeView.setFitHeight(15);
        unSeeBtn.setGraphic(unseeView);
    }
}
